<?php

class CplangsController extends Core_Controller_Action 
{
	private $_api;
	
	public function init() {	
		$this->_api = new Langs();
	}
	public function indexAction(){
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		
		$pagerLayout = $this->_api->getListWithPager( $page, $this->view->baseUrl() );
		
		$this->view->list = $pagerLayout->execute();
		$this->view->pager = $pagerLayout;
		
		$currency = new Currency();
		$cur = $currency->getList();
		$list = array();
		foreach($cur as $jeden)
			$list[$jeden['cur_id']]=$jeden['name'];  
		$this->view->currency_list=$list;
		
	}
	public function addAction(){
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		if($this->_request->isPost())
		{
			if($id){
				$langs = $this->_api->getItem( $id )->getFirst();
			}
			else{
				$langs = $this->_api;
			}
			$langs->name_short = $this->_getParam('name_short');
			$langs->name_long = $this->_getParam('name_long');
			$langs->currency_cur_id = $this->_getParam('currency_cur_id');
			$langs->save();
			if(!$id)
			{
				$prod = new Products();
				$list_prod = $prod->getList();
				foreach($list_prod as $one)
				{
					$des = new DescriptionProducts();
					$des->products_p_id = $one->p_id;
					$des->langs_l_id = $langs->l_id;
					$des->save();
				}
			}
			$this->_redirect('cplangs');
		}
		if($id)
		{
			$langs = $this->_api->getItem( $id )->getFirst()->toArray();
			$this->view->langs = $langs;
		}
		$currency = new Currency();
		$cur = $currency->getList();
		$list = array();
		foreach($cur as $jeden)
			$list[$jeden['cur_id']]=$jeden['name'];  
		$this->view->currency_list=$list;
	}
	public function deleteAction(){
		$id = intval( $this->_request->getParam( 'id', 0 ) );
		if($id){
			$this->_api->deleteItem($id);
			$des = new DescriptionProducts();
			$des->deleteLang($id);
		}
		$this->_redirect('cplangs');
	}
}
	